﻿var RELEASEFLAG = true              //在该文件init()赋值
var DEBUG_FLAG = true               //在该文件init()赋值
var PC_TEST = false                 //在该文件init()赋值
var WITH_MOD_MSG = true             //在该文件init()赋值
var WITH_MOD_CALL = false;          //在该文件init()赋值
var WITH_MOD_SETTINGS = false;      //在该文件init()赋值
var SIP_DIRECT = false;             //在该文件init()赋值
var SIP_NUM_NAME =""                //宽带宝号,家世通号等等
var APP_MAIN_NAME =""               //家世通等,在该文件init()赋值
var DEFALT_VIDEO_CALL=false         //默认是否打开视频
var HD_VERSION=false                //宽屏(HD)模式适用于PAD版本
var TV_COMMON =false                //TV模式
var FAKE_MOBILE=false               //在PC上模拟移动设备
var CQ_HAIGUAN_VERSION=true;        //在该文件init()赋值
var WITH_MOD_PTT=false;              //true为ptt，false为conference
var WITH_MOD_DESKTOP=false;          //是否显示桌面tab
var NJ_IMS_CONTREX_VERSION = false;  //是否是话务台版本
var STANDARD_VERSION=true             //loogear标准版
var ONLYCALL_VERSION=false             //只有通话
var WITH_MOD_CONFERENCE = false
var WITH_MOD_CONTACT = false
var HNTJ_VERSION = false
var NAVY_VERSION = false

var inited=init();
function init(){
//    var  config=softPhoneManager.loadGolbalConfig();

    //console.log("config.RELEASEFLAG="+config.RELEASEFLAG);
//    RELEASEFLAG = config.RELEASEFLAG;
//    DEBUG_FLAG = config.DEBUG_FLAG;
//    PC_TEST = config.PC_TEST;
//    WITH_MOD_MSG = config.WITH_MOD_MSG;
//    WITH_MOD_CALL = config.WITH_MOD_CALL;
//    WITH_MOD_SETTINGS = config.WITH_MOD_SETTINGS;
//    SIP_DIRECT = config.SIP_DIRECT;
//    SIP_NUM_NAME =config.SIP_NUM_NAME;
//    APP_MAIN_NAME =config.APP_MAIN_NAME;
//    DEFALT_VIDEO_CALL=config.DEFALT_VIDEO_CALL;
//    HD_VERSION= config.HD_VERSION;
//    TV_COMMON = config.TV_COMMON
//    FAKE_MOBILE=config.FAKE_MOBILE;
//    CQ_HAIGUAN_VERSION=config.CQ_HAIGUAN_VERSION;
//    WITH_MOD_PTT = config.WITH_MOD_PTT;
//    WITH_MOD_DESKTOP = config.WITH_MOD_DESKTOP
//    NJ_IMS_CONTREX_VERSION = config.NJ_IMS_CONTREX_VERSION
//    STANDARD_VERSION = config.STANDARD_VERSION
//    ONLYCALL_VERSION = config.ONLYCALL_VERSION
//    WITH_MOD_CONFERENCE = config.WITH_MOD_CONFERENCE
//    WITH_MOD_CONTACT = config.WITH_MOD_CONTACT
//    HNTJ_VERSION = config.HNTJ_VERSION
//    NAVY_VERSION = config.NAVY_VERSION
}

function isMobileDevice()
{
    if(FAKE_MOBILE) return true;
    return (Qt.platform.os == "android" || Qt.platform.os == "ios");
}

function isAndroid()
{
    return Qt.platform.os == "android"
}

function isIos() {
    return Qt.platform.os == "ios"
}
function isMacOS() {
    return Qt.platform.os == "osx"
}
function isWindows() {
    return Qt.platform.os == "windows"
}
function isHDScreen() {
    return  HD_VERSION;
}
function isTVCommon(){
     return TV_COMMON
}
function isContain(element,array){
    for(var i = 0; i < array.length; i++){
        if(element == array[i]){
            return true;
        }
    }
    return false;
}
function isEmpty(object){
    var reg = new RegExp("^[ ]+$")
    if(object == "" || object == undefined || object == null || reg.test(object)) return true
    else return false;
}

function leftListWidth(){
    return m_lineValue*5;
}
function leftListHeight(){
    return m_lineValue*1.25;
}

function timeday( timestr ){
    var time = Qt.formatDateTime(timestr,"yyyy:MM:dd");
    console.log("time---"+time+"===="+Qt.formatDateTime(timestr,"hh:mm"))
    var curDate = new Date();
    var preDate = new Date(curDate.getTime() - 24*60*60*1000);
    var yesterday = Qt.formatDateTime(preDate,"yyyy:MM:dd")
    if( time === Qt.formatDateTime(new Date(),"yyyy:MM:dd")) return Qt.formatDateTime(timestr,"hh:mm");//qsTr("今天")
    else if( time === yesterday ) return qsTr("昨天")
    else if( time.substring(0,4) === curDate.getFullYear().toString())
    {
        return Qt.formatDateTime(time,qsTr("M月d日"))
    }
    else{
        return Qt.formatDateTime(time,qsTr("yyyy年M月d日"))
    }
}

function log() {
    if (DEBUG_FLAG) {
        var result = ""
        for (var i = 0;i < arguments.length;i++) {
            result += (arguments[i] + ((i == arguments.length - 1) ? "" : ";"))
        }
        console.log(result)
    }
}

function coalesce(arg1, arg2, arg3) {
    if(arg1 !== ""&&arg1 !== null&&arg1 !== undefined)
        return arg1
    if(arg2 !== ""&&arg2 !== null&&arg2 !== undefined)
        return arg2
    return arg3
}

var leftMargin = (Qt.platform.os==="ios"?5:10)
var rightMargin = 10

var titleColor = "#fafafa"
var titleHeight = 88

var contactCurrentPageColor = "green"
var contactdefaultPageColor = "#e1e1e1"

var marginValue = 20

var leftMarginScale = 0.21
var rightMarginScale = 0.21

function toBit2(num) {
    return ((num > 9) ? num : ("0" + num))
}

function toHMS(seconds) {
    if (seconds < 60)
        return ("00:" + toBit2(seconds))

    if ((60 <= seconds) && (seconds < 3600)) {
        var m = parseInt(seconds / 60)
        var s = seconds % 60
        return toBit2(m) + ":" + toBit2(s);
    }else if((seconds > 3600)){
        var h = parseInt(seconds / 3600)
        var m =parseInt((seconds-h*3600)/60)
        var s = seconds % 60
        return toBit2(h) + ":"+toBit2(m) + ":" + toBit2(s);
    }
}

function createXmlHttpRequest() {
    return new XMLHttpRequest()
}

function timeday( time, now ){
    var curDate = new Date();
    var preDate = new Date(curDate.getTime() - 24*60*60*1000);
    var yesterday = Qt.formatDateTime(preDate,"yyyy:MM:dd")
    if( time === Qt.formatDateTime(new Date(),"yyyy:MM:dd")) return qsTr(now)
    else if( time === yesterday ) return qsTr("昨天")
    else if( time.substring(0,4) === curDate.getFullYear().toString())
    {
        return Qt.formatDateTime(time,qsTr("M月d日"))
    }
    else{
        return Qt.formatDateTime(time,qsTr("yyyy年M月d日"))
    }
}

function pushView(component, objects, url, parent, properties) {
    component = Qt.createComponent(url)
    if(component.status == Component.Ready) {
        var object = component.createObject(parent, properties)
        objects.splice(objects.length, 0, object)
        object.z=99
        return object
    }else{
        console.log("ERROR:load "+url+" Error string:"+component.errorString());
    }
}

function replaceView(component, objects, url, parent, properties) {
    if(objects.length > 0)
        popView(objects)
    return pushView(component, objects, url, parent, properties)
}

function popView(objects) {
    if(objects.length > 0) {
        var deleted = objects.splice(-1, 1)
        deleted[0].destroy()
    }
}

function popAllView(objects) {
    while(objects.length>0) {
        var deleted = objects.pop();
        deleted.destroy()
    }
}

function pushIndexView(component,index, objects, url, parent, properties)
{
    component = Qt.createComponent(url)
    if(component.status == Component.Ready) {
        var object = component.createObject(parent,properties)
        object.z=99
        objects.push({mark:index,object:object})
    }else{
        console.log("ERROR:load "+url+" Error string:"+component.errorString());
    }
}

function popIndexView(index,objects)
{
    for(var i = 0 ; i< objects.length;i++)
    {
        if(objects[i].mark == index)
        {
            objects[i].object.destroy()
            objects.splice(i,1)
            break
        }
    }
}

function showPopDialog(parent_compent,data,saveFunction,cancelFunction){
    var component = Qt.createComponent("qrc:/qml/common/PopInputDialog.qml");
    if (component.status == Component.Ready){
        var obj =component.createObject(parent_compent, {"x": 0, "y": 0,"z":99,
                                            "width":parent_compent.width,
                                            "height":parent_compent.height,
                                            "data_p":data,
                                            "savecallback":function callback(value){
                                                obj.destroy();
                                                if(saveFunction!=null)
                                                    saveFunction(value);
                                            },
                                            "cancelcallback":function _callback(){
                                                obj.destroy();
                                                if(cancelFunction!=null)
                                                    cancelFunction();
                                            }
                                        });
    }else{
        console.log("Error loading component:", component.errorString());
    }

}
function showPopWin(parent_compent,url,data,finishedFunction){
    var component = Qt.createComponent(url);
    if (component.status === Component.Ready){
        var obj =component.createObject(parent_compent, {"x": 0, "y": 0,"z":parent_compent.z+1,
                                            "width":parent_compent.width,
                                            "height":parent_compent.height,
                                            "data_p":data,
                                            "finishedcallback":function callback(value){
                                                obj.destroy();
                                                if(finishedFunction!=null)
                                                    finishedFunction(value);
                                            }
                                        });
        return obj;
    }else{
        console.log("Error loading component:", component.errorString());
    }
}

function showPopWin2(parent_compent,url,data,finishedFunction,cancelfunction){
    var component = Qt.createComponent(url);
    var  topObjectName="__topOBJECTxx";//避免重复加载
    var tmp = parent_compent.children
    var oldObj=null;
    for (var i = 0; i < tmp.length; ++i){
        if(tmp[i].objectName!=undefined&&tmp[i].objectName==topObjectName){
            oldObj= tmp[i];
        }
    }
    if(oldObj!=null){
        oldObj.destroy();
    }

    data["x"]=0;
    data["y"]=0;
    data["z"]=parent_compent.z+1;
    //    data["width"]=parent_compent.width;
    //    data["height"]=parent_compent.height;
    data["objectName"]=topObjectName;


    data["finishedcallback"]=function callback(value){
        obj.destroy();
        if(finishedFunction!=null)
            finishedFunction(value);
    };
    data["cancelfunction"]=function callback(value){
        obj.destroy();
        if(cancelfunction!=null)
            cancelfunction(value);
    };

    if (component.status === Component.Ready){
        var obj =component.createObject(parent_compent,data);
        return obj;
    }else{
        console.log("Error loading component:", component.errorString());
    }

}

// 显示会议demo
function showMeetingDemo()
{
    var component = Qt.createComponent("qrc:/qml/demo/MeetingMain.qml")
    if(component.status === Component.Ready)
    {
        var object = component.createObject()
        object.show()
    }
    else
    {
        console.log("ERROR:load MeetingMain.qml"+" Error string:"+component.errorString());
    }
}
